package com.songlanyun.modules.refillcard.controller.admin;

import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import com.songlanyun.modules.refillcard.entity.SaleRefillCardEntity;
import com.songlanyun.modules.refillcard.service.SaleRefillCardService;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.user.exception.UserException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 充值卡
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-12 10:30:59
 */
@RestController
@RequestMapping("admin/refill/card")
public class SaleRefillCardAdminController extends AbstractController {
    @Autowired
    private SaleRefillCardService saleRefillCardService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = saleRefillCardService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SaleRefillCardEntity saleRefillCard = saleRefillCardService.getById(id);

        return R.ok().put("data", saleRefillCard);
    }

    /**
     * 保存
     */
    @SysLog("保存充值卡")
    @PostMapping("/save")
    public R save(@RequestBody SaleRefillCardEntity saleRefillCard) {
        verifyAdmin();//校验用户
        ValidatorUtils.validateEntity(saleRefillCard, AddGroup.class);//校验数据

        saleRefillCardService.save(saleRefillCard);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改充值卡")
    @PutMapping("/update")
    public R update(@RequestBody SaleRefillCardEntity saleRefillCard) {
        verifyAdmin();//校验用户
        ValidatorUtils.validateEntity(saleRefillCard, UpdateGroup.class);//校验数据
        saleRefillCardService.updateById(saleRefillCard);

        return R.ok();
    }

    /**
     * 删除
     */
    @SysLog("删除充值卡")
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        verifyAdmin();
        saleRefillCardService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 上下架
     */
    @SysLog("上下架充值卡")
    @PostMapping("/operation")
    public R operation(@RequestBody SaleRefillCardEntity saleRefillCard) {
        verifyAdmin();//校验用户
        if (saleRefillCard.getAdded() == null) {
            throw new RRException(GoodsExceptionEnum.OPERATION_NOT_NULL);
        }
        saleRefillCardService.updateById(saleRefillCard);

        return R.ok();
    }


}
